package com.wz.netty.transport;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class TransPortService {

	private int port;

	private EventLoopGroup bossGroup = new NioEventLoopGroup();
	private EventLoopGroup workerGroup = new NioEventLoopGroup();
	private ServerBootstrap b = new ServerBootstrap();

	private ChannelFuture channelFuture;
	public TransPortService(int port){
		this.port = port;
	}

	static class MyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter{

		@Override
		public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
			System.out.println("init1-channelRegistered");
		}

		@Override
		public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
			System.out.println("init1-channelUnregistered");
		}

		@Override
		public void channelActive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("init1-channelActive");
		}

		@Override
		public void channelInactive(ChannelHandlerContext ctx) throws Exception {
			System.out.println("init1-channelInactive");
		}

		@Override
		public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
			System.out.println("init1-channelRead");
		}

		@Override
		public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
			System.out.println("init1-channelReadComplete");
		}

		@Override
		public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
			System.out.println("init1-userEventTriggered");
		}

		@Override
		public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
			System.out.println("init1-channelWritabilityChanged");
		}

		@Override
		public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
			System.out.println("init1-exceptionCaught");
		}
		
	}
	
	public void run() throws Exception{
		try{
			b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

				@Override
				protected void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(new TransportServerHandler());
				}
			}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
//			b.handler(new MyChannelInboundHandlerAdapter());
			channelFuture = b.bind(port).sync();
			channelFuture.channel().closeFuture().sync();
		} catch(Exception e){
			e.printStackTrace();
		} finally {
			workerGroup.shutdownGracefully();
			bossGroup.shutdownGracefully();
		}
	}

	public void stop(){
		channelFuture.channel().close();
	}

	public static void main(String[] args) {
		TransPortService portService = new TransPortService(8001);
		try {
			portService.run();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}